package com.zfzs.post.modules.agent.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.zfzs.post.modules.agent.domain.AgentMoney;
import com.zfzs.post.modules.agent.repository.AgentMoneyRepository;
import com.zfzs.post.modules.agent.service.AgentMoneyService;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyDTO;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentMoneyMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import com.zfzs.post.modules.agent.domain.AgentMoney;
import com.zfzs.post.modules.agent.repository.AgentMoneyRepository;
import com.zfzs.post.modules.agent.service.AgentMoneyService;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyDTO;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentMoneyMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import com.zfzs.post.modules.agent.repository.AgentMoneyRepository;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyDTO;
import com.zfzs.post.modules.agent.service.dto.AgentMoneyQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Optional;

/**
* @author ck
* @date 2019-08-07
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentMoneyServiceImpl implements AgentMoneyService {

    @Autowired
    private AgentMoneyRepository agentMoneyRepository;

    @Autowired
    private AgentMoneyMapper agentMoneyMapper;

    @Override
    public Object queryAll(AgentMoneyQueryCriteria criteria, Pageable pageable){
        Page<AgentMoney> page = agentMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentMoneyMapper::toDto));
    }

    @Override
    public Object queryAll(AgentMoneyQueryCriteria criteria){
        return agentMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
    }

    @Override
    public AgentMoney findById(Long id) {
        Optional<AgentMoney> agentMoney = agentMoneyRepository.findById(id);
        if(agentMoney.isPresent()){
            return agentMoney.get();
        }else{
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentMoney create(AgentMoney resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return agentMoneyRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentMoney resources) {
        Optional<AgentMoney> optionalAgentMoney = agentMoneyRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalAgentMoney,"AgentMoney","id",resources.getId());
        AgentMoney agentMoney = optionalAgentMoney.get();
        agentMoney.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        agentMoney.copy(resources);
        agentMoneyRepository.save(agentMoney);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentMoneyRepository.deleteById(id);
    }

    @Override
    public int withdrawMoney(Long id, BigDecimal money, Long version) {
        return agentMoneyRepository.withdrawMoney(id,money,version);
    }

    @Override
    public void addAgentMoney(Long agentId, BigDecimal money) {
        AgentMoney agentMoney = findById(agentId);
        agentMoney.setTotalMoney(agentMoney.getTotalMoney().add(money));
        agentMoney.setUsableMoney(agentMoney.getUsableMoney().add(money));
        update(agentMoney);
    }
}